package com.nttuan.util;

import android.content.Context;
import android.graphics.Paint;
import android.util.Log;
import android.widget.TextView;

import com.nttuan.model.dao.ContactDao;
import com.nttuan.model.dao.service.ContactService;
import com.nttuan.model.entity.Contact;
import com.nttuan.model.entity.Task;
import com.nttuan.model.entity.User;
import com.nttuan.mycalender.R;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by TuanNT on 11/20/2014.
 */
public class EmailParser {
//    public static void strike(TextView view, boolean isOn){
//        if(isOn)
//            view.setPaintFlags(view.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
//        else
//            view.setPaintFlags(view.getPaintFlags() & ~Paint.STRIKE_THRU_TEXT_FLAG);
//    }
    public static String buildTask(Context context,Task task){
        StringBuilder sb = new StringBuilder();

        String location = "";
        String note ="";
        StringBuilder repeat = new  StringBuilder();
        StringBuilder contacts = new StringBuilder();


        if(!task.getLocation().getAddress().equals(""))
            location = "<tr>\n" +
                    "\t\t\t<td  bgcolor='#919191'><font size='3'>Địa điểm</font></td>\n" +
                    "\t\t\t<td><font size='3'>"+task.getLocation().getAddress()+"</font></td>\n" +
                    "\t\t</tr>";
        if(!task.getNote().equals(""))
            note = "<tr>\n" +
                    "\t\t\t<td  bgcolor='#919191'><font size='3'>Ghi chú</font></td>\n" +
                    "\t\t\t<td><font size='3'>"+task.getNote()+"</font></td>\n" +
                    "\t\t</tr>";
        if(task.getRepeat() !=null && !task.getRepeat().equals("")){
            String[] dateOfWeek = context.getResources().getStringArray(R.array.dayOfWeek);
            repeat.append("<tr>\n" +
                    "\t\t\t<td  bgcolor='#919191'><font size='3'>Lăp lại</font></td>\n" +
                    "\t\t\t<td><font size='3'>");
            String[] arr = task.getRepeat().split(",");
            String temp;
            for(String sDay: arr){
                repeat.append(dateOfWeek[Integer.parseInt(sDay)] + " - ");
            }
            sb.append("</font></td>\n" +
                    "\t\t</tr>");
        }
        contacts.append("<tr>\n" +
                "\t\t\t<td bgcolor='#919191'><font size='3'>Người tham gia</font></td>\n" +
                "\t\t\t<td>\n" +
                "\t\t\t\t<ul>");
        for(Contact contact: task.getListContact()){
            User user = contact.getUser();
            contacts.append("<li><font size='3'><font color='red'> "+user.getUserName()+" </font> - (<b>email</b>:"+user.getEmail()+" - <b>diện thoại</b>:"+user.getPhone()+" - <b>Địa chỉ</b>: "+user.getAddress()+")</font></li>");
        }
        sb.append("<table style='width:auto' border =1>\n" +
                "\t\t<tr >\n" +
                "\t\t\t<td colspan=2 style='width:120px' bgcolor='#919191'><font size='4'><b><center>"+task.getTaskName()+"</center></b></font></td>\n" +
                "\t\t\t\n" +
                "\t\t</tr>\n" +
                "\t\t<tr>\n" +
                "\t\t\t<td  bgcolor='#919191'><font size='3'>Bắt đầu</font></td>\n" +
                "\t\t\t<td><font size='3'>"+Support.displayDate(task.getStart())+" - "+ Support.displayTime(task.getStart()) +"</font></td>\n" +
                "\t\t</tr>\n" +
                "\t\t<tr>\n" +
                "\t\t\t<td  bgcolor='#919191'><font size='3'>Kết thúc</font></td>\n" +
                "\t\t\t<td><font size='3'>"+Support.displayDate(task.getEnd())+" - "+ Support.displayTime(task.getEnd()) +"</font></td>\n" +
                "\t\t</tr>\n" +
                 repeat+
                 location +
                 note +
                 contacts.toString() +
                "\t</table>");
        return sb.toString();
    }
    public static String buildContent(Context context, List<Task> list, String requestBy, Date onDate){
        StringBuilder sb = new StringBuilder();
        ContactService contactService = ContactDao.getInstance(context);
        StringBuilder dataContent = new StringBuilder();
        for(Task task: list){
            task.setListContact(contactService.contactsByTask(task.getTaskId()));
            dataContent.append(buildTask(context, task));
            dataContent.append("<br>");
            dataContent.append("<br>");

        }


        sb.append("<html>\n" +
                "<head>\n" +
                "<meta charset=\"UTF-8\">\n" +
                "</head>\n" +
                "<body>\n" +
                "<p><b>Sự kiện trong ngày:</b> <mark>"+Support.displayDate(onDate)+"</mark>&nbsp;&nbsp;&nbsp; <b>Nhận yêu cầu từ: </b><mark>"+requestBy+"</mark><p>"+
                 dataContent.toString() +
                "</body>\n" +
                "</html>");


       return sb.toString();
    }
    public static String displayRepeat(Context context, String repeat){
        if(repeat.equals(""))
            return null;
        String[] arr = repeat.split(",");
        String[] dateOfWeek = context.getResources().getStringArray(R.array.dayOfWeekShort);
        StringBuilder result = new StringBuilder();
        if(arr.length > 6)
            return context.getText(R.string.every_date).toString();
        else {
            int count;
            for (count = 0; count < arr.length - 1; count++) {
                result.append(dateOfWeek[Integer.parseInt(arr[count])] + context.getText(R.string.separation_date));
            }
            result.append(dateOfWeek[Integer.parseInt(arr[count++])]);
        }
        return result.toString();
    }
}
